<?php
	include_once('PHPExcel/Classes/PHPExcel.php');
	class ShopController extends  BaseController{

		protected  $_className ="Shop";

		public function getShopList($filter=null,$state){
			if($filter == null){
				$filter  = new SqlFilter();
			}
			$filter->addFilter("shop_type",EQUALS,SHOP);
			return parent::getEntityList($filter,$state);
		}

		public function getShopById($id){
			return parent::getEntityByid($id);
		}


		public function getPurchaseList($filter=null,$state){

			if($filter == null){
				$filter  = new SqlFilter();
			}
			$filter->addFilter("shop_type",EQUALS,PURCHASE);
			return parent::getEntityList($filter,$state);
		}

		public function getDistributionCenter($filter=null,$state){
			if($filter == null){
				$filter  = new SqlFilter();
			}
			$filter->addFilter("shop_type",EQUALS,DC);
			return parent::getEntityList($filter,$state);
		}
		
		public function getDeptList($filter=null,$state){
			
			return parent::getEntityList($filter,$state);
		}



		public function saveShop($transaction = null){
			$this->_bean->set("shop_type",SHOP);
			parent::save($post_json,$transaction);
			$this->sync2Wechat($this->_bean->get("id"));
	    }

	    public function deleteShop($id,$transaction){
	    	$this->_bean = parent::getEntityByid($id);
	    	if($this->_bean!=null){
	    		parent::delete($id);
	    		$dept = new Department(AccessTokenManager::getAccessToken());
	    		$rtn = $dept->deleteDepartment($this->_bean->get("wechat_id"));
	    		if($rtn["errcode"]!=0){
	    			  throw new CustomerException($rtn["errmsg"],5);
	    		}
	    	}
			
			
	    }

	    public function batchDelete($transaction){
	    	$shopList = parent::getBeanList(null,new SqlFilter("a.state",EQUALS,-1));
	    	
	    	if($shopList!=null){
	    		$dept = new Department(AccessTokenManager::getAccessToken());
	    		for($i=0;$i<count($shopList);$i++){
		    		$rtn = $dept->deleteDepartment($shopList[$i]->get("wechat_id"));
		    		if($rtn["errcode"]!=0){
		    			  throw new CustomerException($rtn["errmsg"],5);
		    		}
	    		}
	    	}
			
	    }



	    public function savePurchase($transaction = null){
			$this->_bean->set("shop_type",PURCHASE);
			parent::save($post_json,$transaction);
	    }

	    public function saveDistributionCenter($transaction = null){
			$this->_bean->set("shop_type",DC);
			parent::save($post_json,$transaction);
	    }




	    public function addShop2Wechat($name,$parentid=1){
			$dept = new Department(AccessTokenManager::getAccessToken());
			$ret = json_decode($dept->createDepartment($name,$parentid));
			
			return $ret->id;		
		}

		public function sync2Wechat($shopId = null){
			$errors = array();
			$wx_dept = new Department(AccessTokenManager::getAccessToken());
			$dept = AbstractBean::LoadBean("Shop");
			$filter = new SqlFilter("a.state",EQUALS,1);
			if($shopId!=null){
				$filter->addFilter("a.id",EQUALS,$shopId);
			}
			$shopList = $dept->fetchBeanList($filter);

			for($i=0;$i<count($shopList);$i++){
				$wechatId  = $shopList[$i]->get("wechat_id");
				$name = $shopList[$i]->get("name")."(".$shopList[$i]->get("company_name").")";
				$parentId = $shopList[$i]->getStringValue("parent_id");
				if($parentId == ""){
					$parentId =1;
				}
				if($wechatId ==  null || $wechatId=="" || $wechatId ==0){
					$ret = json_decode($wx_dept->createDepartment($name,$parentId));
				}
				else{
					$ret = json_decode($wx_dept->updateDepartment($wechatId,$name,$parentId));
				}
				if($ret->errcode == 0 && $shopList[$i]->getStringValue("wechat_id")==""){
					$shopList[$i]->set("wechat_id",$ret->id);
					$shopList[$i]->save();
				}
				else{
					array_push($errors,"[".$name."]:".$ret->errmsg);
				}
			}
			return  $errors;
		}

	

	    public function batchImport($excel,$transaction){
			
			if(!empty($excel) &&count($excel)>0){
				
				  $objReader = PHPExcel_IOFactory::createReader('Excel2007');
		          $objReader->setReadDataOnly(true);
		          $objPHPExcel = $objReader->load($excel[0]["file_path"]);
		          $objPHPExcel->setActiveSheetIndex(0);
		          $objWorksheet = $objPHPExcel->getActiveSheet();         
		          $highestRow = $objWorksheet->getHighestRow(); 
		          $highestColumn = $objWorksheet->getHighestColumn(); 
		          $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 
				  $deptList = array();		          
		          for ($row = 2; $row <= $highestRow; $row++) { 
		                $bean = AbstractBean::LoadBean("Shop");
		                $code = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
		                if($code ==  null ||  $code==""){
		                	continue;
		                }
		                if(!AbstractBean::UniqueValidate("Shop","code",$code)){
		                	throw new CustomerException("门店编号[".$code."]已经存在",3);
		                }
		                $bean->set("code",$code);
		                $bean->set("name",$objWorksheet->getCellByColumnAndRow(1, $row)->getValue());
		                $bean->set("company_name",$objWorksheet->getCellByColumnAndRow(2, $row)->getValue());
		                $bean->set("address",$objWorksheet->getCellByColumnAndRow(3, $row)->getValue());
		                $bean->set("zipcode",$objWorksheet->getCellByColumnAndRow(4, $row)->getValue());
		                $bean->set("contact",$objWorksheet->getCellByColumnAndRow(5, $row)->getValue());
		                $bean->set("contact_phone",$objWorksheet->getCellByColumnAndRow(6, $row)->getValue());
		                $bean->save();
		         }
		         Shop::batchUpdateCompanyID();
		         $this->sync2Wechat();

			}
		}



		


		
	}

?>